package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.*;
import com.indusfo.spc.pojo.Workshop;
import com.indusfo.spc.service.WorkshopService;
import com.indusfo.spc.vo.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author louk
 * @date 2019/11/14 15:46
 */
@Service
public class WorkshopServiceImpl implements WorkshopService {


    private  static  final Logger logger = LoggerFactory.getLogger(WorkshopServiceImpl.class);

    @Resource
    private WorkshopMapper workShopMapper;

    @Resource
    private ProductlineMapper productlineMapper;

    @Resource
    private StationMapper stationMapper;

    /**
     * 新增工位信息
     * @author chenm
     * @create 2019-07-13 15:19
     * @param workShop
     * @return
     */
    @Override
    public JSONObject insertWorkShop(Workshop workShop) {
        try {
            if(workShop.getWorkshopId() != null) {
                throw new ParamsErrorException("新增操作不能选择车间");
            }
            checkParam(workShop);
            checkNoRepeat(workShop);
            int	row = workShopMapper.insertSelective(workShop);

            if(row == 0) {
                throw new ModifyFailedException("数据新增失败！");
            }
            return JSONObject.oK("新增成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 删除车间信息
     * @author chenm
     * @create 2019-07-13 17:19
     * @return
     */
    @Transactional
    @Override
    public JSONObject delWorkShop(Integer[] workshopIds, Integer dataState) {
        try {
            if(dataState == null) {
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if (workshopIds == null || workshopIds.length==0) {
                throw new ParamsErrorException("请选择要删除的车间信息");
            }
            /**
             * @author: louk
             * @Description: 判断所删除的车间下是否含有产线
             * @date: 2019/9/6 10:52
             *
             */
            if(dataState==2){
                int stationCount = productlineMapper.listProductLineByWorkShopId(workshopIds);
                if(stationCount>0){
                    throw new ParamsErrorException("所选车间含有产线无法删除");
                }
            }

            // 执行存储过程
            int row = workShopMapper.delWorkshop(workshopIds, dataState);
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数dataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                }
            }
            if(workshopIds!=null && workshopIds.length>0){
                productlineMapper.deleteProDuctLineByWorkShopId(workshopIds,dataState);
                stationMapper.updateStationByWorkshopId(workshopIds,dataState);
//                proListMapper.updateProListByWorkshopId(workshopIds,dataState);
            }

            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }


    /**
     * 更新
     *
     * @author chenm
     * @date 2019/07/15 13:38
     * @param [productLine]
     * @return com.indusfo.spc.vo.JSONObject
     */
    @Override
    public JSONObject updateWorkShop(Workshop workShop) {
        try {
            if(workShop.getWorkshopId() == null) {
                throw new ParamsErrorException("请选择要修改的车间资料");
            }
            checkParam(workShop);
            checkNoRepeat(workShop);
            int	row = workShopMapper.updateByPrimaryKeySelective(workShop);

            if(row == 0) {
                throw new ModifyFailedException("数据更新失败！");
            }
            return JSONObject.oK("更新成功！");
        } catch (GlobalException e) {  //这里只捕获自定义异常
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     * 查询
     *
     * @author chenm
     * @create 2019-07-13 18:27
     */
    @Override
    public JSONObject queryWorkShop(Workshop workShop) {
        List<Workshop> workShops = new ArrayList<>();
        try {
            Integer pagesize = workShop.getPagesize();
            Integer pageindex = workShop.getPageindex();
            if(pagesize != null && pageindex != null) {
                workShop.setIncept(pagesize*(pageindex - 1));
            }
            Integer workshopId = workShop.getWorkshopId();
            //如果有id,则查询单个产线信息
            if(workshopId != null) {
                workShops = workShopMapper.getWorkshop(workshopId);
            } else{  //查询多个sop管理信息
                workShops = workShopMapper.listWorkshop(workShop);
            }
            if (workShops.isEmpty()) {
                return JSONObject.oK("没有车间相关数据", workShops, 0);
            }
            //查询分页总记录数
            int count = workShopMapper.countWorkshop(workShop);
            return JSONObject.oK("查询成功", workShops, count);
        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(403, e.getMessage());
        }
    }

    /**
     *  @create 2019-07-13 15:19
     * 根据编码查询工位
     * @param workshopCode
     * @param workShopId
     * @return
     */
    @Override
    public Workshop getByWorkShopCode(String workshopCode, Integer workShopId) {
        return workShopMapper.queryByWorkshopCode(workshopCode,workShopId);
    }


    /**
     * 验证信息
     * @author chenm
     * @create 2019-07-13 15:19
     */
    public void checkParam(Workshop workShop){
        if(StringUtils.isEmpty(workShop.getWorkshopCode())){
            throw new ParamsErrorException("车间编码不能为空");
        }
        if(getByWorkShopCode(workShop.getWorkshopCode(),workShop.getWorkshopId()) != null){
            throw new ParamsErrorException("车间编码已存在");
        }
        if(StringUtils.isEmpty(workShop.getWorkshopName())){
            throw new ParamsErrorException("车间名称不能为空");
        }

        if(workShop.getWorkshopCode().length() > 30) {
            throw new ParamsErrorException("车间编码字数超过规定长度30");
        }
        if(workShop.getWorkshopName().length() > 50) {
            throw new ParamsErrorException("车间名称字数超过规定长度50");
        }
        if(workShop.getRemark().length() > 50) {
            throw new ParamsErrorException("备注说明字数超过规定长度50");
        }

    }

    /**
     * 车间名称去重
     * @param workShop
     */
    public void checkNoRepeat(Workshop workShop){
        int count= workShopMapper.selectByWorShopName(workShop);
        if(count>0){
            throw new ParamsErrorException("车间名称已存在");
        }

    }
    @Override
    public List<Workshop> getList() {
        return workShopMapper.listWorkshop(new Workshop());

    }
    @Override
    public String checkExcelContent(Workshop workShop){
        if(StringUtils.isBlank(workShop.getWorkshopCode()) && StringUtils.isBlank(workShop.getWorkshopName())){
            return "该行数据为空(原因:清空行内容时未清空彻底/未输入)";
        }
        if(StringUtils.isEmpty(workShop.getWorkshopCode())){
            return "车间编码不能为空";
        }
        if(getByWorkShopCode(workShop.getWorkshopCode(),workShop.getWorkshopId()) != null){
            return "车间编码已存在";
        }
        if(StringUtils.isEmpty(workShop.getWorkshopName())){
            return "车间名称不能为空";
        }

        if(workShop.getWorkshopCode().length() > 30) {
            return "车间编码字数超过规定长度30";
        }
        if(workShop.getWorkshopName().length() > 50) {
            return "车间名称字数超过规定长度50";
        }
        int count= workShopMapper.selectByWorShopName(workShop);
        if(count>0){
            return "车间名称已存在";
        }
        return null;
    }
}
